package b3_priorityqueue;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;

/**
 * 测试优先级队列，实现Comparator比较器
 */
public class TestPriorityQueue_3 {
    public static void main(String[] args) {

        Person per1 = new Person("小黑", 19);
        Person per2 = new Person("小白", 20);
        Person per3 = new Person("小绿", 18);

        /************************/
        //利用数组排序
        Person[] pers = {per1, per2, per3};
        Arrays.sort(pers, new PersonCom()); //升序
        System.out.println(Arrays.toString(pers));
        Arrays.sort(pers, new PersonComDesc()); //降序
        System.out.println(Arrays.toString(pers));

        /************************/
        //优先级队列排序
        Queue<Person> queue = new java.util.PriorityQueue<>(new PersonCom());
        //入队
        queue.offer(per1);
        queue.offer(per2);
        queue.offer(per3);
        //出队
        pers[0] = queue.poll();
        pers[1] = queue.poll();
        pers[2] = queue.poll();
        System.out.println(Arrays.toString(pers));

        /************************/
        //比较器的匿名内部类写法
        Queue<Animal> queue2 = new PriorityQueue<>(new Comparator<Animal>() {
            @Override
            public int compare(Animal o1, Animal o2) {
                return o1.getWeight() - o2.getWeight();
            }
        });
        /**
         * Lambda表达式，函数式编程，更加简洁的写法。
         * Queue<Animal> queue2 = new PriorityQueue<>(((o1, o2) -> o1.getWeight()-o2.getWeight()));
         */
        //创建Animal对象
        Animal animal1 = new Animal("狗狗", 30);
        Animal animal2 = new Animal("猫猫", 10);
        Animal animal3 = new Animal("猪猪", 50);
        //入队
        queue2.offer(animal1);
        queue2.offer(animal2);
        queue2.offer(animal3);
        //出队
        Animal[] animals = new Animal[3];
        animals[0] = queue2.poll();
        animals[1] = queue2.poll();
        animals[2] = queue2.poll();
        System.out.println(Arrays.toString(animals));
    }
}

// 大 - 小 > 0， 用于升序比较
class PersonCom implements Comparator<Person> {
    @Override
    public int compare(Person o1, Person o2) {
        return o1.getAge() - o2.getAge();
    }
}

// 大 - 小 < 0， 用于降序比较
class PersonComDesc implements Comparator<Person> {
    @Override
    public int compare(Person o1, Person o2) {
        return o2.getAge() - o1.getAge();
    }
}

//自定义Person类
class Person {

    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

//自定义Animal类
class Animal {

    private String name;
    private int weight;

    public Animal(String name, int weight) {
        this.name = name;
        this.weight = weight;
    }

    public int getWeight() {
        return weight;
    }

    @Override
    public String toString() {
        return "Animal{" +
                "name='" + name + '\'' +
                ", weight=" + weight +
                '}';
    }
}